<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class RunGameRank extends Model
{
    public $timestamps = false;
    protected $table = "run_game_rank";


    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'c_id');
    }

    /**
     * 修改排行榜积分
     * @param $user_id
     * @param $integral
     * @return array
     * @throws \Exception
     */
    static function AddRank($user_id, $integral)
    {
        $time = time();
        $thisSeason = RunGameSeason::ThisSeason();
        DB::beginTransaction();

        //找到记录
        $log = DB::table('run_game_rank')->select('id', 'integral')->where('user_id', $user_id)->where('season_id', $thisSeason)->lockForUpdate()->first();
        if (!$log) {
            $log = new RunGameRank();
            $log->user_id = $user_id;
            $log->season_id = $thisSeason;
            $log->integral = $integral;
            $log->created_at = $time;
            $log->updated_at = $time;
            if (!$log->save()) {
                DB::rollBack();

                return error('rank积分修改失败1');
            }
            DB::commit();

            return success(['message' => '成功']);
        }

        if (!DB::table('run_game_rank')->where('id', $log->id)->update([
            'integral' => $log->integral + $integral,
            'updated_at' => $time
        ])) {
            DB::rollBack();

            return error('rank积分修改失败');
        }
        DB::commit();

        return success(['message' => '成功']);

    }


    /**
     * 排行榜
     * @param $user_id
     * @param $thisSeason
     * @return array
     */
    static function Rank($user_id, $thisSeason)
    {
        $rank = RunGameRank::where('season_id', $thisSeason)->orderByDesc('integral')->orderBy('updated_at')->pluck('id')->toArray();
        //自己的排名
        $selfInfo = User::select('name', 'c_avatar')->where('c_id', $user_id)->first();
        $self = RunGameRank::select('id', 'integral')->where('season_id', $thisSeason)->where('user_id', $user_id)->first();
        if ($selfInfo) {
            if (!$self) {
                //本赛季自己没有参加比赛
                $selfRank = [
                    'name' => $selfInfo->name,
                    'avatar' => $selfInfo->c_avatar,
                    'integral' => 0,
                    'rank' => 0,
                ];
            } else {
                $selfRank = [
                    'name' => $selfInfo->name,
                    'avatar' => $selfInfo->c_avatar,
                    'integral' => $self->integral,
                    'rank' => array_keys($rank, $self->id)[0] + 1,
                ];
            }
        } else {
            $selfRank = [
                'name' => '',
                'avatar' => '',
                'integral' => 0,
                'rank' => 0,
            ];
        }

        //积分榜前二十名
        $ranks = RunGameRank::with('user:c_id,name,c_avatar')->where('season_id', $thisSeason)->orderByDesc('integral')->orderBy('updated_at')->skip(0)->take(20)->get();
        $data = [];
        if (count($ranks)) {
            foreach ($ranks as $rank) {
                $data[] = [
                    'name' => $rank->user->name,
                    'avatar' => $rank->user->c_avatar,
                    'integral' => $rank->integral,
                ];
            }
        }
        return success(compact('selfRank', 'data'));
    }


    /**
     * 自己的排名
     * @param $user_id
     * @return int
     */
    static function SelfRank($user_id)
    {
        $thisSeason = RunGameSeason::ThisSeason();
        $rank = RunGameRank::where('season_id', $thisSeason)->orderByDesc('integral')->orderBy('updated_at')->pluck('id')->toArray();
        //自己的排名
        $self = RunGameRank::where('season_id', $thisSeason)->where('user_id', $user_id)->value('id');
        if (User::where('c_id', $user_id)->exists()) {
            if ($self) {
                return array_keys($rank, $self)[0] + 1;
            }
        }
        return count($rank) + 1;
    }
}
